package com.devtty.collabhr.ui;

import java.util.Arrays;

import com.devtty.collabhr.model.Call;
import com.devtty.collabhr.util.ExampleUtil;
import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.BaseTheme;

public class QuickCallWindow extends Window{
	
	Call call;
	
	public QuickCallWindow(){
		setModal(true);
		setCaption("Neuer Anruf");
		setDescription("Bitte geben Sie den Anrufer und eine kurze Beschreibung zum Inhalt des Telefonats an.");
		setHeight("230px");
		setWidth("500px");
		setScrollable(false);
		setResizable(false);
		
		VerticalLayout layout = new VerticalLayout();
		layout.setSizeFull();
		layout.setMargin(false, true, false, true);
		
		call = new Call();
		BeanItem<Call> callItem = new BeanItem<Call>(call);
		
		final Form callForm = new Form();
		callForm.setSizeFull();
		callForm.setWriteThrough(false);
		callForm.setInvalidCommitted(false);
		
		callForm.setFormFieldFactory(new CallFieldFactory());
		callForm.setItemDataSource(callItem);
		
		callForm.setVisibleItemProperties(Arrays.asList(new String[]{"phone", "comment"}));
		//addComponent(callForm);
		layout.addComponent(callForm);
		setContent(layout);
		
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setSpacing(true);
		buttons.setWidth("100%");
		
		/*Button discardChanges = new Button("Abbrechen",
				new Button.ClickListener() {
					public void buttonClick(ClickEvent event) {
						callForm.discard();
					}
				});
		discardChanges.setStyleName(BaseTheme.BUTTON_LINK);
		buttons.addComponent(discardChanges);
		buttons.setComponentAlignment(discardChanges, Alignment.MIDDLE_RIGHT);
		*/
		Button apply = new Button("Speichern",
				new Button.ClickListener() {
					public void buttonClick(ClickEvent event) {
						callForm.commit();
					}
				});
		//buttons.setSizeFull();
		
		buttons.addComponent(apply);
		buttons.setComponentAlignment(apply, Alignment.MIDDLE_RIGHT);
		callForm.getFooter().addComponent(buttons);
		callForm.getFooter().setMargin(false, false, true, true);
		callForm.getFooter().setWidth("100%");
	}
	
	private class CallFieldFactory extends DefaultFieldFactory{

		final ComboBox phone = new ComboBox("Anrufer");
		
		public CallFieldFactory(){
			phone.setContainerDataSource(ExampleUtil.getPhone());
			phone.setFilteringMode(ComboBox.FILTERINGMODE_CONTAINS);
			
			phone.setRequired(true);
			phone.setRequiredError("ist Pflicht");
			//phone.addValidator(new StringLengthValidator("asd", 2, 50, false));
		}
		
		public Field createField(Item item, Object propertyId, Component uiContext){
			Field f;
			if("phone".equals(propertyId)){
				f = phone;
				f.setWidth("100%");
			}else if("comment".equals(propertyId)){
				f = new TextArea("Nachricht");
				f.setWidth("100%");
			}else{
				f = super.createField(item, propertyId, uiContext);
			}
			return f;
		}
		
	}
	
}